<?php

namespace App\Http\Controllers\BusinessData;

use App\Http\Conf\ResultInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Business\Payer;
use App\Http\Models\Business\RetailContractIndicator;
use App\Http\Models\CategoryContract;
use App\Http\Requests\Business\RetailContractIndicatorRequest;
use App\Imports\RetailContractIndicatorImport;
use Exception;
use Maatwebsite\Excel\Facades\Excel;

class RetailContractIndicatorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $year = $request->year ? $request->year : date('Y');
        $category_contract_id = $request->category_contract_id ? $request->category_contract_id : 0;
        $payer_name = $request->payer_name ? $request->payer_name : '';

        $where = [];
        $where['year'] = $year;
        if ($category_contract_id != 0) {
            $where['category_contract_id'] = $category_contract_id;
        }
        $query = RetailContractIndicator::where($where);

        if ($payer_name != '') {
            $payer = Payer::where('name', 'like', '%' . $payer_name . '%')->pluck('code')->toArray();
            if (count($payer) == 0) {
                $query->where(['payer_code' => '']);
            } else {
                $query->whereIn('payer_code', $payer);
            }
        }

        $models = $query->paginate(config('report.quantity_per_page'));

        $categoty_contracts = CategoryContract::all();

        return view(
            'business_data.retail_contract_indicator.index',
            [
                'year' => $year,
                'category_contract_id' => $category_contract_id,
                'payer_name' => $payer_name,
                'category_contracts' => $categoty_contracts,
                'models' => $models
            ]
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categoty_contracts = CategoryContract::all();
        return view('business_data.retail_contract_indicator.create', [
            'category_contracts' => $categoty_contracts
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RetailContractIndicatorRequest $request)
    {
        $model = RetailContractIndicator::where([
            'year' => $request->year,
            'payer_code' => $request->payer_code,
            'category_contract_id' => $request->category_contract_id
        ])->first();

        if ($model) {
            return redirect()->back()->with('fail', '该指标已经存在！');
        }
        $model = new RetailContractIndicator();
        $model->year = $request->year;
        $model->payer_code = $request->payer_code;
        $model->category_contract_id = $request->category_contract_id;
        $model->amount = $request->amount;
        if ($model->save()) {
            return redirect()->action('BusinessData\RetailContractIndicatorController@show', $model)->with('success', '添加成功！');
        } else {
            return '保存零售合同信息有异常！';
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = RetailContractIndicator::find($id);
        return view('business_data.retail_contract_indicator.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $model = RetailContractIndicator::find($id);
        $categoty_contracts = CategoryContract::all();
        return view('business_data.retail_contract_indicator.edit', [
            'model' => $model,
            'category_contracts' => $categoty_contracts
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(RetailContractIndicatorRequest $request, $id)
    {
        $model = RetailContractIndicator::find($id);
        $model->year = $request->year;
        $model->payer_code = $request->payer_code;
        $model->category_contract_id = $request->category_contract_id;
        $model->amount = $request->amount;
        if ($model->save()) {
            return redirect()->action('BusinessData\RetailContractIndicatorController@show', $model)->with('success', '修改成功！');
        } else {
            return '修改零售合同信息有异常！';
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = RetailContractIndicator::find($id);
        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }


    public function import(Request $request)
    {
        if ($request->method() == 'GET') {
            return view('business_data.retail_contract_indicator.import');
        } elseif ($request->method() == 'POST') {
            if ($request->upload_file == null) {
                return redirect()->back()->with('fail', '请选择文件');
            }

            try {
                Excel::import(new RetailContractIndicatorImport, request()->file('upload_file'));
            } catch (Exception $e) {
                return redirect()->back()->with('fail', $e->getMessage());
            }
            return redirect()->action('BusinessData\RetailContractIndicatorController@index')->with('success', '导入数据成功！');
        }
    }
}
